package com.oracle.mall.controller;

import com.alibaba.fastjson.JSON;

import com.oracle.mall.po.CartDetail;
import com.oracle.mall.po.Member;
import com.oracle.mall.po.Nav;
import com.oracle.mall.po.ProductPage;
import com.oracle.mall.service.CartService;
import com.oracle.mall.service.NavService;


import com.oracle.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 控制层 依赖业务层
 */
@Controller
public class IndexController {

    @Autowired
    private CartService cartService;

    @Autowired
    private ProductService productService;

    @Autowired
    private NavService navService;

    @RequestMapping("/index")
    public String index(Model model, Integer cid, Integer bid, String keyWord, @RequestParam(required = false,defaultValue = "1") Integer page, HttpSession session){//参数应该还有page和字符串

        if(session.getAttribute("member")!=null){
            Member member = (Member) session.getAttribute("member");
            Integer mid = member.getMid();
            CartDetail cartDetail = cartService.queryTotal(mid);
            model.addAttribute("cartDetail",cartDetail);
        }

        ProductPage productPage = productService.query(cid,bid,keyWord,page);
        model.addAttribute("productPage",productPage);

        List<Nav> nav = navService.getNav();
        //将查询到的导航数据存入request作用域
        model.addAttribute("nav",nav);
        return "index";


    }

}
